//
//  ViewImageScaleViewController.m
//  MingShi
//
//  Created by MJiao on 15/6/28.
//  Copyright (c) 2015年 MJiao. All rights reserved.
//

#import "ViewImageScaleViewController.h"
#import "KVNProgress.h"

@interface ViewImageScaleViewController ()

@end

@implementation ViewImageScaleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.bigImageView setHidden:YES];

    [KVNProgress showWithStatus:@"" onView:self.loadingView];
    [self.scrollview setUserInteractionEnabled:NO];
    //大图
//    [self.bigImageView setImageWithURL:[NSURL URLWithString:self.imageUrl] placeholderImage:nil success:^(UIImage *image) {
//        [KVNProgress dismiss];
//        [self.ivViewedImage setImageWithURL:[NSURL URLWithString:self.imageUrl] placeholderImage:self.ivViewedImage.image];
//        [self.scrollview setUserInteractionEnabled:YES];
//
//    }                          failure:^(NSError *error) {
//        [KVNProgress dismiss];
//        [self.scrollview setUserInteractionEnabled:YES];
//        [KVNProgress showErrorWithStatus:@"获取大图失败"];
//    }];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:self.smallImageUrl]];
    [request addValue:@"image/*" forHTTPHeaderField:@"Accept"];
    UIImage *cachedImage = [[[UIImageView class] sharedImageCache] cachedImageForRequest:request];

    [self.ivViewedImage setImageWithURL:[NSURL URLWithString:self.imageUrl] placeholderImage:cachedImage success:^(UIImage *image) {
        [KVNProgress dismiss];
        [self.ivViewedImage setImageWithURL:[NSURL URLWithString:self.imageUrl] placeholderImage:self.ivViewedImage.image];
        [self.scrollview setUserInteractionEnabled:YES];

    }                          failure:^(NSError *error) {
        [KVNProgress dismiss];
        [self.scrollview setUserInteractionEnabled:YES];
        [KVNProgress showErrorWithStatus:@"获取大图失败"];
    }];
    //小图
//    [self.ivViewedImage setImageWithURL:[NSURL URLWithString:self.imageUrl] placeholderImage:[UIImage imageNamed:@"loading"]];

    self.scrollview.contentSize = self.ivViewedImage.image.size;
    self.scrollview.delegate = self;
    //    self.scrollView.minimumZoomScale = 1.0;
    self.scrollview.maximumZoomScale = 10;

}

//- (void)viewDidAppear:(BOOL)animated
//{
//    [super viewDidAppear:animated];
//    CGPoint centerPoint = CGPointMake(CGRectGetMidX(self.scrollview.bounds),
//                                      CGRectGetMidY(self.scrollview.bounds));
//    [self view:self.ivViewedImage setCenter:centerPoint];
//}

- (void)view:(UIView *)view setCenter:(CGPoint)centerPoint {
    CGRect vf = view.frame;
    CGPoint co = self.scrollview.contentOffset;

    CGFloat x = centerPoint.x - vf.size.width / 2.0;
    CGFloat y = centerPoint.y - vf.size.height / 2.0;

    if (x < 0) {
        co.x = -x;
        vf.origin.x = 0.0;
    }
    else {
        vf.origin.x = x;
    }
    if (y < 0) {
        co.y = -y;
        vf.origin.y = 0.0;
    }
    else {
        vf.origin.y = y;
    }

    view.frame = vf;
    self.scrollview.contentOffset = co;
}

// MARK: - UIScrollViewDelegate
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return self.ivViewedImage;
}

- (void)scrollViewDidZoom:(UIScrollView *)sv {
    UIView *zoomView = [sv.delegate viewForZoomingInScrollView:sv];
    CGRect zvf = zoomView.frame;
    if (zvf.size.width < sv.bounds.size.width) {
        zvf.origin.x = (sv.bounds.size.width - zvf.size.width) / 2.0;
    }
    else {
        zvf.origin.x = 0.0;
    }
    if (zvf.size.height < sv.bounds.size.height) {
        zvf.origin.y = (sv.bounds.size.height - zvf.size.height) / 2.0;
    }
    else {
        zvf.origin.y = 0.0;
    }
    zoomView.frame = zvf;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
